package main.utils 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	/**
	 * ...
	 * @author Fern
	 */
	public class LoadResource 
	{	
		/**
		 * 加载可视资源
		 * 返回Loader
		 * onLoadComplete，onIOError有一个参数接受Event
		 */
		public static function loadVisualResource(url:String, onLoadComplete:Function = null,
												  applicationDomain:ApplicationDomain = null, onIOError:Function = null):Loader
		{
			var loader:Loader = new Loader();
			var urlRequest:URLRequest = new URLRequest(url);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onVisualResourceLoaded);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onVisualResourceLoaded);		
			
			if (applicationDomain)
			{
				loader.load(urlRequest, new LoaderContext(false, applicationDomain));
			}
			else
			{
				loader.load(urlRequest, new LoaderContext(false, ApplicationDomain.currentDomain));
			}
			function onVisualResourceLoaded(e:Event):void
			{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onVisualResourceLoaded);
				loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onVisualResourceLoaded);
				if (e.type == Event.COMPLETE)
				{
					if (onLoadComplete != null)
					{
						onLoadComplete(e);
					}
				}
				else if (e.type == IOErrorEvent.IO_ERROR)
				{
					if (onIOError != null)
					{
						onIOError(e);
					}
					else
					{
						trace("IOError Info: " + (e as IOErrorEvent).text);
					}
				}
			}
			return loader;
		}			
	}

}